#
# Cookbook Name:: was8.5
# Recipe:: prepare install
#
# Copyright 2013, IBM Corporation
#
# All rights reserved - Do Not Redistribute
#

cuser = node[:chef][:user]
cgroup = node[:chef][:group]

was_tmp_dir = node[:common][:inst_temp] + "/was"

marker_file="/repository.config"

directory "#{was_tmp_dir}" do
  user cuser
  action :create
end

fileset_ctr=0
[
  { repo: "#{was_tmp_dir}/#{node[:was][:was855_tmp_dir]}",
    remote_repo: "#{node[:was][:was855_files_repo]}/#{node[:was][:was855_extracted_dir_optional]}", 
    remote_files: [
      "#{node[:was][:was855_files_repo]}/#{node[:was][:was855_part1_download_file]}",
      "#{node[:was][:was855_files_repo]}/#{node[:was][:was855_part2_download_file]}"]},
  { repo: "#{was_tmp_dir}/#{node[:was][:wasnd_tmp_dir]}/#{node[:was][:base_repo]}",
    remote_repo: "#{node[:was][:wasnd_base_files_repo]}/#{node[:was][:wasnd_fp_extracted_dir_optional]}", 
    remote_files: [
      "#{node[:was][:wasnd_base_files_repo]}/#{node[:was][:wasnd_base_part1_download_file]}",
      "#{node[:was][:wasnd_base_files_repo]}/#{node[:was][:wasnd_base_part2_download_file]}",
      "#{node[:was][:wasnd_base_files_repo]}/#{node[:was][:wasnd_base_part3_download_file]}"]},
  { repo: "#{was_tmp_dir}/#{node[:was][:wasnd_tmp_dir]}/#{node[:was][:fp_repo]}",
    remote_repo: "#{node[:was][:wasnd_fp_files_repo]}/#{node[:was][:wasnd_fp_extracted_dir_optional]}", 
    remote_files: [
      "#{node[:was][:wasnd_fp_files_repo]}/#{node[:was][:wasnd_fp_part1_download_file]}", 
      "#{node[:was][:wasnd_fp_files_repo]}/#{node[:was][:wasnd_fp_part2_download_file]}"]},
  { repo: "#{was_tmp_dir}/#{node[:was][:wasnd_suppl_tmp_dir]}/#{node[:was][:base_repo]}",
    remote_repo: "#{node[:was][:wasnd_suppl_base_files_repo]}/#{node[:was][:wasnd_suppl_base_extracted_dir_optional]}", 
    remote_files: [
      "#{node[:was][:wasnd_suppl_base_files_repo]}/#{node[:was][:wasnd_suppl_base_part1_download_file]}",
      "#{node[:was][:wasnd_suppl_base_files_repo]}/#{node[:was][:wasnd_suppl_base_part2_download_file]}",
      "#{node[:was][:wasnd_suppl_base_files_repo]}/#{node[:was][:wasnd_suppl_base_part3_download_file]}"]},
  { repo: "#{was_tmp_dir}/#{node[:was][:wasnd_suppl_tmp_dir]}/#{node[:was][:fp_repo]}",
    remote_repo: "#{node[:was][:wasnd_suppl_fp_files_repo]}/#{node[:was][:wasnd_suppl_fp_extracted_dir_optional]}", 
    remote_files: [
      "#{node[:was][:wasnd_suppl_fp_files_repo]}/#{node[:was][:wasnd_suppl_fp_part1_download_file]}",
      "#{node[:was][:wasnd_suppl_fp_files_repo]}/#{node[:was][:wasnd_suppl_fp_part2_download_file]}"]},
  { repo: "#{was_tmp_dir}/#{node[:was][:java7_tmp_dir]}/#{node[:was][:base_repo]}",
    remote_repo: "#{node[:was][:java7_base_files_repo]}/#{node[:was][:java7_base_extracted_dir_optional]}", 
    remote_files: [
      "#{node[:was][:java7_base_files_repo]}/#{node[:was][:java7_base_part1_download_file]}",
      "#{node[:was][:java7_base_files_repo]}/#{node[:was][:java7_base_part2_download_file]}",
      "#{node[:was][:java7_base_files_repo]}/#{node[:was][:java7_base_part3_download_file]}"]},
  { repo: "#{was_tmp_dir}/#{node[:was][:java7_tmp_dir]}/#{node[:was][:fp_repo]}",
    remote_repo: "#{node[:was][:java7_fp_files_repo]}/#{node[:was][:java7_fp_extracted_dir_optional]}", 
    remote_files: [
      "#{node[:was][:java7_fp_files_repo]}/#{node[:was][:java7_fp_part1_download_file]}",
      "#{node[:was][:java7_fp_files_repo]}/#{node[:was][:java7_fp_part2_download_file]}"]}
  ].each do |fileset|
  
  fileset_ctr = fileset_ctr + 1
  
  repo = fileset[:repo]
  remote_repo = fileset[:remote_repo]
  remote_files = fileset[:remote_files]
  
  bash "Attempting to fetch extracted repo dir: #{remote_repo}" do
    user cuser
    cwd was_tmp_dir
    code <<-EOH
      (mkdir -p #{repo}
      rsync -r #{node[:common][:scp_repo]}/#{remote_repo}/ #{repo}/
      if [ $? == 0 -a -f #{repo}/#{marker_file} ]; then
        touch /tmp/fetch-#{fileset_ctr}.done
      fi
      ) > /tmp/bashlog-repo-#{fileset_ctr}.log 2>&1
    EOH
  end
  
  ctr = 0
  remote_files.each do |file|
    ctr = ctr+1
    bash "Fetching and extracting: #{file}" do
      user cuser
      cwd repo # Note that the repo dir will have been created in the previous step even if rsync failed
      code <<-EOH
        (set -e
        if [ ! -f "/tmp/fetch-#{fileset_ctr}.done" ]; then
          rsync #{node[:common][:scp_repo]}/#{file} .
          unzip -o $(basename #{file})
          if [ #{ctr} == #{remote_files.size} ]; then
            touch /tmp/fetch-#{fileset_ctr}.done
          fi
        else
          echo "skipping download since repository is already present"
        fi
        ) > /tmp/bashlog-file-#{fileset_ctr}-#{ctr} 2>&1
      EOH
    end 
  end
end